﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Xml;

namespace MovieMetaDataFinder.TheMovieDB
{

    public static class TheMovieDB
    {
        public static byte[] GetBytesFromStringCallingWeb(string url)
        {
            try
            {
                byte[] response = null;

                response = ASCIIEncoding.Default.GetBytes(GetStringFromWeb(url));

                return response;
            }
            catch
            {
                return null;
            }
        }


        private static string GetStringFromWeb(string url)
        {
            try
            {
                string result;
                WebResponse objResponse;
                WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
                objResponse = objRequest.GetResponse();

                using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
                {
                    result = sr.ReadToEnd();

                    sr.Close();
                }
                return result;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return null;
        }

        public static Title GetTheMovieDBData(string xmlData)
        {
            try
            {
                Title response = null;
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlData);

                if (doc != null)
                {
                    response = new Title();

                    response.TheMovieDBID = doc.SafeGetInt("//movie/id");
                    response.Description = doc.SafeGetString("//movie/short_overview");
                    response.BackdropURL = doc.SafeGetString("//movie/backdrop[@size='original']");
                    response.IMDBID = doc.SafeGetString("//movie/imdb");


                    if (!string.IsNullOrEmpty(response.Description))
                    {
                        response.Description = response.Description.Replace("\n\n", "\n");
                    }

                    foreach (XmlNode n in doc.SelectNodes("//people/person[@job='director']/name"))
                    {
                        string name = n.InnerText.Trim();

                        if (!string.IsNullOrEmpty(name))
                        {
                            response.Persons.Add(new Title.Person(name, Title.PersonType.Director, "Director"));
                        }
                    }

                    foreach (XmlNode n in doc.SelectNodes("//people/person[@job='author']/name"))
                    {
                        string name = n.InnerText.Trim();

                        if (!string.IsNullOrEmpty(name))
                        {
                            response.Persons.Add(new Title.Person(name, Title.PersonType.Writer, "Writer"));
                        }
                    }

                    foreach (XmlNode n in doc.SelectNodes("//people/person[@job='actor']"))
                    {
                        string name = n.SafeGetString("name");
                        string role = n.SafeGetString("role");

                        if (!string.IsNullOrEmpty(name))
                        {
                            response.Persons.Add(new Title.Person(name, Title.PersonType.Actor, role));
                        }
                    }

                }
                return response;
            }
            catch { return null; }
        }
    }
}